var hosturl = "http://127.0.0.1:8395"
// var hosturl = "http://111.229.24.16:8395"

var serviceurl = {
    login: "/login",
    loginstate: "/login/state",
    reg: "/reg",
    devicestates: "/device/list",
    alarmlist: "/alarm/list",
    deviceadd: "/device/add",
    devicedel: "/device/del",
    deviceinfo: "/device/info",
    changepassword: "/user/set/password",
    // getligincaptcha: "/get_ligin_captcha",
    // getregcaptcha: "/get_reg_captcha",
    devicedashboard: "/device/dashboard",
    getruaptcha: "/ruaptcha",
    userinfo:"/user/info",
    sendverifyemail:"/user/send_verify_email",
    verifyemail:"/user/verify_email",
    retrievepassword: "/user/retrieve_password",
    resetpassword: "/user/reset_password",

}

function getServiceURL(service) {
    return hosturl + serviceurl[service];
}


function setSession(loginResp) {
    window.localStorage.setItem("token", loginResp.token);
    window.localStorage.setItem("user_id", loginResp.id);
}

function clearSession() {
    window.localStorage.clear();
}



function getCurToken() {
    return window.localStorage.getItem("token");
}



function getCurUsetID() {
    return window.localStorage.getItem("user_id");
}


(function() {
    function AjaxRequest(opts) {
        this.type = opts.type || "get";
        this.url = opts.url;
        this.param = opts.param || {};
        // this.isShowLoader = opts.isShowLoader || false;
        this.dataType = opts.dataType || "json";
        this.callBack = opts.callBack;
        this.init();
    }

    AjaxRequest.prototype = {
        //初始化
        init: function() {
            this.sendRequest();
        },
        //发送请求
        sendRequest: function() {
            var self = this;
            $.ajax({
                type: this.type,
                headers: {
                    'access-token': getCurToken(),
                },
                url: this.url,
                data: this.param,
                dataType: this.dataType,
                // async: false,
                success: function(res) {
                    if (res != null && res != "") {
                        if (self.callBack) {
                            if (Object.prototype.toString.call(self.callBack) === "[object Function]") {
                                // if (res.code == 900) {
                                //     clearSession();
                                //     top.location.href = 'login.html';
                                // }
                                self.callBack(res);
                            } else {
                                clearSession();
                                self.callBack({ "code": -1, "msg": "网络连接异常！" });
                            }
                        }
                    }
                },
                error: function(XMLHttpRequest, textStatus, errorThrown) {
                    try{
                        if(XMLHttpRequest.responseJSON.code == 900){
                            clearSession();
                        }else{
                            // self.callBack({ "code": -1, "msg": "服务器连接失败！" });
                            clearSession();
                            self.callBack(XMLHttpRequest.responseJSON);
                        }   
                    } catch (e) {
                        clearSession();
                    }
  
                },
                // fail: function(){
                // 	alert('网络不行，请求错误');
                // }
            });
        }
    };

    window.AjaxRequest = AjaxRequest;
})();

//获取url中的参数
function getUrlParam(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
    var r = window.location.search.substr(1).match(reg); //匹配目标参数
    if (r != null) return unescape(r[2]);
    return null; //返回参数值
}



// 退出登录
function logout() {
    clearSession();
    window.location.href = '/login.html';
}


// 检查登录状态
function isLoginStats() {
    var curToken = getCurToken();
    if (curToken == "" || curToken == null) {
        window.location.href = '/login.html';
    } else {
        new AjaxRequest({
            type: "get",
            url: getServiceURL('loginstate'),
            param: "",
            callBack: function(res) {
                if (res.code == 900) {
                    clearSession();
                    top.location.href = 'login.html';
                }
                return true;
            }
        });
    }

}

// $('#logout').click(function () {
// 	clearSession();
// 	window.location.href = '/login.html';
// });


// 设备状态

function getDeviceStateText(state){
    var result = "";
    switch (state) {
        case 1:
            result = "已激活";
            break;
        case 2:
            result = "未激活";
            break;
        default:
            result = "其他";
            break;
    }
    return result;
}